#include "Stdafx.h"
#include <xcommondef.h>
#include <xcommonfunc.h>
#include <xinternet.h>
#include "MainDlg.h"
#include "XUpdate.h"


#define UPDATE_URL	L"http://www.ioxiu.com/download/update.ini"
#define UPDATE_TEXT_URL L"http://www.ioxiu.com/download/update_%d.ini"

DWORD CheckForUpdate()
{
	DWORD dwType = REG_DWORD;
	DWORD dwSize = sizeof(DWORD);
	DWORD dwValue = 0;
	if (ERROR_SUCCESS == SHGetValue(HKEY_CURRENT_USER, L"software\\ioxiu\\Easy-PrintScreen", L"updatever", &dwType, &dwValue, &dwSize))
	{
	}

	CString strVer;
	BOOL bRet = GetFileContentFromInternet(UPDATE_URL, strVer);
	if (!bRet) return 0;

	DWORD dwVersion = _wtoi(strVer);
	if (dwVersion > dwValue)
	{
		return dwVersion;
	}
	else
	{
		SHSetValue(HKEY_CURRENT_USER, L"software\\ioxiu\\Easy-PrintScreen", L"updatever", dwType, &dwVersion, dwSize);
	}

	return 0;
}

BOOL CheckForNewVersion(TCHAR * pszVersion)
{
	CString strVersion;
	if (pszVersion)
	{
		strVersion = pszVersion;
	}
// 	DWORD dwRet = CheckForUpdate();
// 	ZERO_THEN_RETURN(dwRet)

	CString strURL = UPDATE_URL;
//	strURL.Format(UPDATE_TEXT_URL, dwRet);


	TCHAR szTempDir[MAX_PATH] = {0};
	::ExpandEnvironmentStrings(L"%temp%", szTempDir, MAX_PATH);

	TCHAR szTempFile[MAX_PATH] = {0};
	GetTempFileName(szTempDir, L"xupdate", 0, szTempFile);

	BOOL bRet = DownloadFileFromInternet(strURL, szTempFile);
	TRUE_OR_RETURN(bRet)

	bRet = ::PathFileExists(szTempFile);
	TRUE_OR_RETURN(bRet)

	TCHAR szVersion[MAX_PATH] = {0};
	GetPrivateProfileString(L"Easy-PrintScreen", L"version", L"1.0.0.0", szVersion, MAX_PATH, szTempFile);

	if (CompareStringVersion(szVersion, strVersion) <= 0)
	{
		return FALSE;
	}

	CString strText;
	strText.Format(CString(MAKEINTRESOURCE(IDS_NEW_VERSION)), L"Easy-PrintScreen", szVersion);
	int nRet = ::MessageBox(NULL, strText, CString(MAKEINTRESOURCE(IDS_UPDATE)), MB_OKCANCEL);
	if (nRet == IDOK)
	{
		TCHAR szURL[MAX_PATH] = {0};
		GetPrivateProfileString(L"Easy-PrintScreen", L"url", L"", szURL, MAX_PATH, szTempFile);

		DownloadNewVersion(szURL);//, dwRet);
	}
	return TRUE;	
}

BOOL DownloadNewVersion(CString strURL)//, DWORD dwVer)
{
	if (strURL.IsEmpty())
	{
		return FALSE;
	}
	CMainDlg dlg(strURL);
	MSG msg;

	// Create & show our main window
	if ( NULL == dlg.Create ( NULL))
	{
		return FALSE;
	}

	dlg.ShowWindow(SW_SHOW);
	dlg.UpdateWindow();

	// Run the message loop
	while ( GetMessage(&msg, NULL, 0, 0) > 0 )
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	BOOL bRet = dlg.GetDownloadStatus();
	if (bRet)
	{
		HWND hWnd = ::FindWindow(NULL, L"Easy-PrintScreen");
		CString str = dlg.GetDownloadFile();
		HINSTANCE hInst = ::ShellExecute(NULL, L"open", dlg.GetDownloadFile(), NULL, NULL, SW_SHOW);
		::PostMessage(hWnd, WM_COMMAND, IDCANCEL, NULL);

// 		DWORD dwType = REG_DWORD;
// 		DWORD dwSize = sizeof(DWORD);
// 		if (ERROR_SUCCESS == SHSetValue(HKEY_CURRENT_USER, L"software\\ioxiu\\Easy-PrintScreen", L"updatever", dwType, &dwVer, dwSize))
// 		{
// 			int a = 0;
// 		}
	}

	return TRUE;
	
}